package com.cheng.io;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;

/**
 * File常用方法
 */
@SuppressWarnings("all")
public class FileTest04 {
    public static void main(String[] args) {
        File file = new File("io/resources");
        // 获取所有该目录下的文件和目录
        File[] files = file.listFiles();
        for (File f : files) {
            String eleName = f.getName();
            System.out.println("eleName = " + eleName);
            if ("testFolder".equals(eleName)){
                // 获取testFolder目录下的子目录和文件
                File[] fs2 = f.listFiles();
                for (File f2 : fs2) {
                    String f2EleName = f2.getName();
                    System.out.println("\t|f2EleName = " + f2EleName);
                }
            }
        }

        System.out.println("======================================");
        File file2 = new File("io/resources");
        // 获取resources目录下的txt格式文件，实现FilenameFilter接口
        File[] files1 = file2.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".txt");
            }
        });

        for (File f : files1) {
            System.out.println(f.getName());
        }
    }
}
